package com.mrjy.video.bo;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.mrjy.common.entity.HappinessInfo;
import com.mrjy.common.entity.IntegralInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.entity.VideoGoodsInfo;
import com.mrjy.integral.dao.IHappinessInfoDao;
import com.mrjy.integral.util.HappinessInfoEnum;
import com.mrjy.integral.util.HappinessInfoUtil;
import com.mrjy.integral.util.IntegralEnum;
import com.mrjy.integral.util.IntegralUtil;
import com.mrjy.video.dao.IVideoGoodsInfoDao;

@Service("videoGoodsBo")
public class VideoGoodsInfoBo implements IVideoGoodsInfoBo {
	@Resource
    private IVideoGoodsInfoDao videoGoodsInfoDao;
    @Resource
	private IHappinessInfoDao happinessInfoDao;

	@Override
    public Object txAddVideoGoods(VideoGoodsInfo goods, UserInfo userInfo) {
        Map<String, Object> map = new HashMap<>();
        if (userInfo == null) {
            return "userInfo_error";
        }

        goods.setUserId(userInfo.getId());
        int count = videoGoodsInfoDao.queryExistByInfo(goods);
        if(count>0){
            map.put("err_code", 1);
            map.put("err_msg", "已经点过赞");
            return map;
        }
        goods.setDisableFlag(0);
        int rows = videoGoodsInfoDao.insert(goods);
        if(rows!=1){
            map.put("err_code", 2);
            map.put("err_msg", "操作失败");
        } else {
        	 //评论回答的操作，新增积分
            IntegralInfo integral = IntegralUtil.setIntegral(goods.getUserId()
    				, IntegralEnum.VIDEO_GOOD.getScore()
    				, IntegralEnum.VIDEO_GOOD.getTypeDesc()
    				, IntegralEnum.VIDEO_GOOD.getTypeId()
    				, goods.getId());
    		
    		//爱值添加
			HappinessInfo hi = HappinessInfoUtil.setHappinessInfo(goods.getUserId()
					, HappinessInfoEnum.VIDEO_GOOD.getLoveValue(), HappinessInfoEnum.VIDEO_GOOD.getTypeDesc()
					, HappinessInfoEnum.VIDEO_GOOD.getTypeId(), "0", goods.getId());
			happinessInfoDao.insert(hi);
            map.put("score", integral.getScore());
            map.put("err_code", 0);
            map.put("err_msg", "点赞成功");
        }
        return map;
    }

	@Override
	public VideoGoodsInfo queryById(int id) {
        return videoGoodsInfoDao.queryById(id);
    }

	@Override
	public void txUpdateById(VideoGoodsInfo goods) {
        videoGoodsInfoDao.updateById(goods);
    }

	@Override
	public int txDeleteByIds(int[] ids) {
		try {
            return videoGoodsInfoDao.deleteByIds(ids);
        } catch (DataAccessException e) {
			return 0;
		}
	}

	@Override
	public int updateDisableFlag(VideoGoodsInfo goods) {
        return videoGoodsInfoDao.updateDisableFlag(goods);
    }

}
